//
//  OneWayMgr.m
//  carcareIOS
//
//  Created by ileo on 15/6/26.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "OneWayMgr.h"
#import "NetWay.h"

@implementation WayGpsInfoModel

-(CLLocationCoordinate2D)coordinate{
    if (_coordinate.latitude == 0) {
        _coordinate = CLLocationCoordinate2DMake(self.lat, self.lng);
    }
    return _coordinate;
}
- (void)setX:(NSInteger)X
{
    _X = X;
    if(X < 0)_type = GPS_DOWN;
    if(X > 0)_type = GPS_UP;
}
- (void)setY:(NSInteger)Y
{
    _Y = Y;
    _type = GPS_TURN;
}
- (void)setZ:(NSInteger)Z
{
    _Z = Z;
    _type = GPS_SHAKE;
}
@end

@interface OneWayMgr()

@property (nonatomic, copy) NSArray *gpss;
@property (nonatomic, copy) NSArray *types;

@end

@implementation OneWayMgr

-(void)getGpsWithStartTime:(NSString *)startTime withEndTime:(NSString *)endTime Success:(void(^)(NSArray *gpss, NSArray *types))success fail:(void(^)())fail
{
    __weak __typeof(self) wself = self;
    //    self.model.start_time = @"20160613190955";
    [[NetWay wayGpsInfosFrom:startTime to:endTime] requestSuccess:^(id JSON) {
        NSArray * gpsJsons = JSON[@"gps"];
        NSDictionary * typeJsons = JSON[@"gsensor"];
        NSArray * speedJsons = JSON[@"speed"];
        NSMutableArray * typeTempArr = [NSMutableArray arrayWithCapacity:[typeJsons[@"X"] count]];
        for (int index = 0; index < [typeJsons allKeys].count; index ++) {
            NSArray * dataArr = typeJsons[[typeJsons allKeys][index]];
            for (int y = 0; y < dataArr.count; y ++) {
                WayGpsInfoModel * model = [[WayGpsInfoModel alloc]initWithDic:dataArr[y]];
                [typeTempArr addObject:model];
                if(model.X < 0){
                    self.speedDownTimes ++;
                }else if(model.X > 0)
                {
                    self.speedUpTimes ++;
                }
            }
            
        }
        NSMutableArray * gpsTempArr = [NSMutableArray arrayWithCapacity:gpsJsons.count    ];
        for (int index = 0; index < gpsJsons.count; index ++) {
            WayGpsInfoModel * model = [[WayGpsInfoModel alloc] initWithDic:gpsJsons[index]];
            if (index == 0) {
                model.beginEnd = GPS_BEGIN;
            }else if (index == gpsJsons.count - 1) {
                model.beginEnd = GPS_END;
            }else{
                model.beginEnd = GPS_ONWAY;
            }
            model.SPEED = [speedJsons[index] integerValue];
            [gpsTempArr addObject:model];
            
        }
        wself.gpss = gpsTempArr;
        wself.types = typeTempArr;
        
        if (success) {
            success(gpsTempArr,typeTempArr);
        }
        
    }];
}

@end